// pages/home/home.js
var http = require('../../utils/httpHelper.js');
var api = require('../../service/api.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        homeCategories: [],
        likeGoods: [],
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getHomeCategory();
    },

    getHomeCategory() {
        var that = this;
        console.log(that);

        api.findHomeCatList().then(resp => {
                if (resp && resp.msg == 'SUCCESS') {
                    var homeCategories = resp.data.filter(item => item.catType == 1);
                    for (var i = 0; i < homeCategories.length; i++) {
                        var homeCategory = homeCategories[i];
                        this.handleHomeCat(homeCategory);
                        this.getHomeCategoryGoods(i, homeCategory);
                    };
                    that.setData({
                        'homeCategories': homeCategories
                    });
                    console.log(that.data);
                }
            },
            resp => {
                console.log(resp);
                wx.showToast({
                    title: '服务器错误',
                    icon: 'loading',
                    duration: 2000
                })
            });
    },
    goSearch() {
        wx.navigateTo({
            url: '/pages/goods/search'
        });
    },
    goodsDetail(event) {
        var goods = event.currentTarget.dataset.goods;
        wx.setStorageSync('goods', goods)
        wx.navigateTo({
            url: '/pages/goods/detail'
        });
        console.log(goods);
    },

    getHomeCategoryGoods(position, homeCategory) {
        var that = this;
        api.findProdListByACatList(homeCategory.catIds, 1, 4).then(resp => {
            if (resp && resp.msg == 'SUCCESS') {
                resp.data.rows.map(item => {
                    if (item.prodImage) {
                        item.prodImages = item.prodImage.split(',');
                        item.firstProdImage = item.prodImages[0];
                    }
                });
                homeCategory.children = resp.data.rows;
                var t = 'homeCategories[' + position + ']';
                that.setData({
                    [t]: homeCategory
                });
            }
        });
    },
    handleHomeCat(homeCategory) {
        let catIds = '',
            catNames = '',
            prodCatList = homeCategory.prodCatList;
        if (!Array.isArray(prodCatList) || prodCatList.length === 0) return;

        prodCatList.map(item => {
            catIds += ',' + item.ID;
            catNames += ',' + item.catName;
        });
        homeCategory.catIds = catIds.replace(',', ''); // trimStart
        homeCategory.catNames = catNames.replace(',', '');
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})